package nic.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import nic.controller.QNapController.NetworkShare;

import org.apache.commons.validator.UrlValidator;

public class AddFtpDialog extends JDialog {

	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JTextField urlJTextField = null;
	private JLabel jLabel1 = null;
	private JComboBox saveFolderJComboBox = null;
	private JCheckBox credentialsJCheckBox = null;
	private JLabel jLabel2 = null;
	private JTextField usernameJTextField = null;
	private JLabel jLabel3 = null;
	private JTextField passwordJTextField = null;
	private Vector<NetworkShare> saveFolders;
	private boolean ok = false;
	private JButton okJButton = null;
	
	/**
	 * This method initializes 
	 * 
	 */
	public AddFtpDialog(JFrame frame, Vector<NetworkShare> saveFolders, String url) {
		super(frame, "Add FTP/HTTP task", false);
		this.saveFolders = saveFolders;
		initialize();
		if (url != null)
		{
			URL url2;
			try {
				url2 = new URL(url);
				String userInfo = url2.getUserInfo();
				if (userInfo != null)
				{
					String[] usrAndPwd = userInfo.split(":");
					String username = usrAndPwd[0];
					String password = "";
					if (usrAndPwd.length > 1)
					{
						password = usrAndPwd[1];
					}
					
					getUrlJTextField().setText(url.replace(userInfo + "@", ""));
					usernameJTextField.setText(username);
					passwordJTextField.setText(password);
					credentialsJCheckBox.setSelected(true);
				} else
				{
					getUrlJTextField().setText(url);
				}
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				getUrlJTextField().setText(url);
			}
		}
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new Dimension(434, 219));
        this.setModal(true);
        this.setContentPane(getJContentPane());
		
        usernameJTextField.setEnabled(credentialsJCheckBox.isSelected());
		passwordJTextField.setEnabled(credentialsJCheckBox.isSelected());
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 0;
			gridBagConstraints14.gridy = 9;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.fill = GridBagConstraints.BOTH;
			gridBagConstraints13.gridy = 8;
			gridBagConstraints13.weightx = 1.0;
			gridBagConstraints13.gridx = 0;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.gridy = 7;
			jLabel3 = new JLabel();
			jLabel3.setText("Password");
			GridBagConstraints gridBagConstraints111 = new GridBagConstraints();
			gridBagConstraints111.gridy = 6;
			gridBagConstraints111.fill = GridBagConstraints.BOTH;
			gridBagConstraints111.gridx = 0;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.gridy = 5;
			jLabel2 = new JLabel();
			jLabel2.setText("Username");
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.gridy = 4;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints7.gridy = 3;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.gridx = 0;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridy = 2;
			jLabel1 = new JLabel();
			jLabel1.setText("Save to network share folder");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Url of the download task");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(jLabel, gridBagConstraints);
			jContentPane.add(getUrlJTextField(), gridBagConstraints1);
			jContentPane.add(jLabel1, gridBagConstraints11);
			jContentPane.add(getSaveFolderJComboBox(), gridBagConstraints7);
			jContentPane.add(getCredentialsJCheckBox(), gridBagConstraints9);
			jContentPane.add(jLabel2, gridBagConstraints10);
			jContentPane.add(getUsernameJTextField(), gridBagConstraints111);
			jContentPane.add(jLabel3, gridBagConstraints12);
			jContentPane.add(getPasswordJTextField(), gridBagConstraints13);
			jContentPane.add(getOkJButton(), gridBagConstraints14);
		}
		return jContentPane;
	}

	/**
	 * This method initializes urlJTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUrlJTextField() {
		if (urlJTextField == null) {
			urlJTextField = new JTextField();
			urlJTextField.setFocusable(true);
		}
		return urlJTextField;
	}

	/**
	 * This method initializes saveFolderJComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getSaveFolderJComboBox() {
		if (saveFolderJComboBox == null) {
			saveFolderJComboBox = new JComboBox();
			
			NetworkShare selected = null;
			for (NetworkShare saveFolder : this.saveFolders)
			{
				saveFolderJComboBox.addItem(saveFolder);
				if (saveFolder.isSelected())
				{
					selected = saveFolder;
				}
			}
			if (selected != null)
			{
				saveFolderJComboBox.setSelectedItem(selected);
			}
		}
		return saveFolderJComboBox;
	}


	/**
	 * This method initializes credentialsJCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getCredentialsJCheckBox() {
		if (credentialsJCheckBox == null) {
			credentialsJCheckBox = new JCheckBox();
			credentialsJCheckBox.setText("Username and passwort needed to access the URL");
			credentialsJCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					usernameJTextField.setEnabled(credentialsJCheckBox.isSelected());
					passwordJTextField.setEnabled(credentialsJCheckBox.isSelected());
				}
			});
		}
		return credentialsJCheckBox;
	}

	/**
	 * This method initializes usernameJTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUsernameJTextField() {
		if (usernameJTextField == null) {
			usernameJTextField = new JTextField();
			usernameJTextField.setFocusable(true);
		}
		return usernameJTextField;
	}

	/**
	 * This method initializes passwordJTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPasswordJTextField() {
		if (passwordJTextField == null) {
			passwordJTextField = new JPasswordField();
			((JPasswordField)passwordJTextField).setEchoChar('*');
			usernameJTextField.setFocusable(true);
		}
		return passwordJTextField;
	}

	public boolean isOk() {
		return ok;
	}
	
	public String getUrl() {
		return urlJTextField.getText();
	}
	
	public NetworkShare getSaveFolder()
	{
		return (NetworkShare)saveFolderJComboBox.getSelectedItem();
	}

	public boolean useCredentials()
	{
		return credentialsJCheckBox.isSelected();
	}
	
	public String getUsername()
	{
		if (credentialsJCheckBox.isSelected())
		{
			return usernameJTextField.getText();
		} else 
		{
			return "";
		}
	}
	
	public String getPassword()
	{
		if (credentialsJCheckBox.isSelected())
		{
			return passwordJTextField.getText();
		} else 
		{
			return "";
		}
	}
	
	/**
	 * This method initializes okJButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkJButton() {
		if (okJButton == null) {
			okJButton = new JButton();
			okJButton.setText("Ok");
			okJButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					String[] schemes = {"http","ftp"};
					UrlValidator urlValidator = new UrlValidator(schemes);
				    if (urlValidator.isValid(getUrl()))
				    {
						AddFtpDialog.this.ok = true;
						AddFtpDialog.this.setVisible(false);
				    } else
				    {
				    	JOptionPane.showMessageDialog(AddFtpDialog.this,
				    			"Please enter an valid url");
				    }
				}
			});
		}
		return okJButton;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
